package com.trainingcloud.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;


@Component
public class MyFileUtil {

    public static String BASE_URL = "D:\\Case\\file";

    public static String fileUpload(String dir, MultipartFile file) {
        File filedir = new File(BASE_URL + "/" +dir);
        //判断文件夹是否存在，不存在就去创建
        if(!filedir.exists()){
            filedir.mkdirs();
        }
        String fileName = getFileName(file.getOriginalFilename());
        File newFile = new File(BASE_URL + "/" +dir + "/" + fileName);
        try {
            file.transferTo(newFile);
            return dir + "/" + fileName;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void fileDownload(String path, HttpServletResponse response) {
        File file = new File(BASE_URL + "/" + path);
        try {
            InputStream fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            response.addHeader("Content-Length",file.length()+"");
            response.addHeader("filename", file.getName());
            response.setContentType("application/octet-stream");
            OutputStream out= new BufferedOutputStream(response.getOutputStream());
            out.write(buffer);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getFileName(String filename){
        String[] split = filename.split("/.");
        String exten = split[split.length-1];
        String name = UUID.randomUUID().toString().replace("-","")+"."+exten;
        return name;
    }

    public static void deleteFile(String fileUrl){
        File file = new File(BASE_URL + "/" + fileUrl);
        file.delete();
    }
    public static void getImg(String path ,HttpServletResponse response){
        File file = new File(BASE_URL + "/" + path);
        System.out.println("-----------------------------------------------");
        System.out.println(file);
        try {
            InputStream fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            response.addHeader("Content-Length",file.length()+"");
            response.addHeader("filename", file.getName());
            response.setContentType("image/png");
            OutputStream out= new BufferedOutputStream(response.getOutputStream());
            out.write(buffer);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
